﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActivitiesControl.Domain.Model;
using ActivitiesControl.Domain.DAL;
using System.Data.SqlClient;
using System.Data;

namespace ActivitiesControl.Repository.DAL.SqlServer
{
    internal class SqlServerResourceRepository
        : SqlServerBaseRepository<Resource>, IResourceRepository
    {
        public void Add(Resource entity)
        {
            throw new NotImplementedException();
        }

        public void Update(Resource entity)
        {
            throw new NotImplementedException();
        }

        public void Remove(Resource entity)
        {
            throw new NotImplementedException();
        }

		public void RemoveById(int id)
		{
			throw new NotImplementedException();
		}

        public Resource GetById(int id)
        {
            try
            {
                Connection.Open();
                SqlCommand cmd = Connection.CreateCommand();
                cmd.CommandText = "select * from Resources where resourceId = @resourceId";
                cmd.Parameters.AddWithValue("resourceId", id);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable result = new DataTable();
                adapter.Fill(result);

                if (result != null && result.Rows != null && result.Rows.Count > 0)
                    return Row2Object(result.Rows[0]);

                return null;
            }
            finally
            {
                Connection.Close();
            }
        }

        public IEnumerable<Resource> GetAll()
        {
            throw new NotImplementedException();
        }

        protected override Resource Row2Object(DataRow row)
        {
            return new Resource
            {
                Id = Convert.ToInt32(row["resourceId"]),
                Name = row["name"].ToString()
            };
        }
	}
}
